﻿package com.PlantVsZombies.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.PlantVsZombies.db.model.Pass;
import com.PlantVsZombies.util.DBUtil;

public class PassDao {
	QueryRunner qr = new QueryRunner(DBUtil.getDataSource());

	public boolean insertPass(Pass pass) {
		String sql = "insert into t_pass(name, time, data, player) values(?,?,?,?)";
		Object[] params = { pass.getName(), pass.getTime(), pass.getData(), pass.getPlayer()};
		try {
			return qr.update(sql, params) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("插入关卡信息失败");
			return false;
		}
	}

	public List<Pass> selectByPlayer(String player) {
		String sql = "select * from t_pass where player = ? order by time";
		try {
			return   (List<Pass>) qr.query(sql, player, new BeanListHandler(Pass.class));
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("查询关卡信息出错");
			return new ArrayList<>();
		}
	}
	
	public Pass select(Integer recordId) {
		String sql = "select * from t_pass where recordId = " + recordId;
		try {
			return  (Pass) qr.query(sql, new BeanHandler(Pass.class));
		} catch (SQLException e) {
			System.out.println("查询关卡信息出错");
			return null;
		}
	}
}
